<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\WxUser;
use App\Models\Phpwarn;
use Cache;

class ProcessAlarm implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    protected $date; 
    
    protected $clearCache = false;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($date=null, $clear=false)
    {
        $this->date = $date;
        
        $this->clearCache = $clear;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $wxusers = WxUser::with(['user'=>function($query){
            $query->select(['userid', 'Co_ID']);
        }])->get()->toArray();
        
        $query =  Phpwarn::query();
        
        $time = date('Y-m-d');
        if($this->date){
           $query->where('pdi_updatetime', 'like', $this->date.'%');
        }else{
           $query->where('pdi_updatetime', '>', $time);
        }
        $alarms = $query->select(['pdi_devid', 'pdi_name', 'Co_ID', 'pdi_warnname', 'pdi_areaname', 'pdi_updatetime', 'pdi_index'])
                ->get()
                ->groupBy('Co_ID')
                ->toArray();
        if(!$alarms){
            return;
        }
        
        foreach ($wxusers as $user) {
            $openid = data_get($user, 'openid');
            $cid = data_get($user, 'user.Co_ID');
            $myAlarm = data_get($alarms, $cid);
            if(!$myAlarm||!$cid){
                continue;
            }

            foreach ($myAlarm as $v) {
                $index = data_get($v, 'pdi_index');
                $cacheKey = 'alarm_'.$cid.'_'.$index;
                if($this->clearCache){
                    Cache::forget($cacheKey);
                }
                if(Cache::get($cacheKey)){
                    continue;
                }
                
                $this->toMessage($openid, $v);
                
                Cache::put($cacheKey, 1, now()->endOfDay());
            }
        }

    }
    
    //发送通知
    protected function toMessage($openid, $data) {
        
        $app = app('wechat.official_account');
        $pdi = data_get($data, 'pdi_devid');
        $url = "http://h5.gzaotian.com/h6/#/alarm/index/".$pdi;
        
        $app->template_message->send([
            'touser' => $openid,
            'template_id' => 'VuEKLojm6LBNZvXLhKJ9dOpmQtdSxzWE87Z_S_ty0fM',
            'url' => $url,
            'data' => [
                'first' => '有新的告警信息，请及时处理',
                'keyword1' => $pdi,
                'keyword2' => data_get($data, 'pdi_areaname').'--'.data_get($data, 'pdi_name'),
                'keyword3' => ['value' => data_get($data, 'pdi_warnname'), 'color'=>"#ff0000"],
                'keyword4' => data_get($data, 'pdi_updatetime'),
                'remark' => '请注意设备维护！',
            ],
        ]);
    }
}
